/**
 * @author lk
 * @date 2022/6/8 16:27
 */
package com.mmall.io;

import org.junit.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.stream.Stream;

public class FilesTest01 {
    @Test
    public void test1() throws IOException {
        Path path = Path.of(".");
        Stream<Path> list = Files.list(path);
        list
                .filter(Files::isDirectory)
//                .filter(Files::isRegularFile)
//                .filter(e->e.getFileName().toString().equalsIgnoreCase("pom"))
                .forEach(System.out::println);
    }

    @Test
    public void testStream() {
        Integer[] ints = {1, 2, 3, 4};
        Stream<Integer> stream = Stream.of(ints);
        System.out.println(stream.filter(e -> e > 1)
                .count());
        Stream.of("kasdjiajskd".split(""))
                .filter(e -> e.compareTo("d") > 0)
                .forEach(System.out::print);

    }

    @Test
    public void testS2() {
        List<String> words = new ArrayList<>();
        words.add("some");
        words.add("how");
        words.add("this");
        words.add("hi");

        Object[] objects = words.stream()
                .map(e -> e.substring(0, 1).toUpperCase(Locale.ROOT).length())
                .peek(e -> {
                    int k = e + 1;
                    System.out.println(k);
                })
                .limit(10)
                .toArray();
        System.out.println(Arrays.toString(objects));
    }

    @Test
    public void testSub() throws IOException {

        List<Path> subFolders = new ArrayList<>();
        Path rootPath = Path.of("src/main/java/com/mmall/thread");
        Optional<Path> first = Files.list(rootPath)
                .peek(e -> {
                    if (Files.isDirectory(e)) {
                        subFolders.add(e);
                    }
                })
                .findFirst();
        System.out.println(first.get());

    }

    @Test
    public void testException() {
        Deque<Integer> stack = new LinkedList<>();
        Integer N = 1000000;
        Instant start = Instant.now();
        for (int i = 0; i < N; i++) {
            ifVersion(stack);
        }

        Instant end = Instant.now();
        System.out.println(Duration.between(start,end).toMillis());

        start = Instant.now();
        for (int i = 0; i < N; i++) {
            catchVersion(stack);
        }

        end = Instant.now();
        System.out.println(Duration.between(start,end).toMillis());

    }

    private void stackPop(Deque<?> stack) {

        for (int i = 0; i < 10000; i++) {
            ifVersion(stack);

            catchVersion(stack);
        }
    }

    private void catchVersion(Deque<?> stack) {
        try {
            stack.pop();
        } catch (Exception e) {
//            System.out.print("");
        }
    }

    private void ifVersion(Deque<?> stack) {
        if (!stack.isEmpty()) {
            stack.poll();
        }
    }


}
